package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

public class OpProc {

	public void So(){
		String propriedades = System.getProperties().toString();
			System.out.println(propriedades);
			String[] vetPropriedades = propriedades.split(",");
			for (String s : vetPropriedades){
				System.out.println(s);	
			}
		String sisOp = System.getProperty("os.name");
		System.out.println(sisOp);
	}

	public void executaProcesso(String caminho){
		try{
		Runtime.getRuntime().exec(caminho);
		} catch(Exception e){
			if(e.getMessage().contains("eleva")){
				StringBuffer sb = new StringBuffer();
				sb.append("cmd/c");
				sb.append(" ");
				sb.append(caminho);
				try {
					Runtime.getRuntime().exec(sb.toString());
				} catch (IOException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null,
							e1.getMessage(), "ERRO",
							JOptionPane.ERROR_MESSAGE);
					
				}
			} else{
				JOptionPane.showMessageDialog(null,
						e.getMessage(), "ERRO",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	public void ListaProcessos(){
		String comando = "tasklist.exe /FO TABLE /NH";
		try {
			Process p = Runtime.getRuntime().exec(comando);
			InputStream fluxo = p.getInputStream();
			InputStreamReader lerFluxo = new InputStreamReader(fluxo);
			BufferedReader bufferLeitura = new BufferedReader(lerFluxo);
			String linha = bufferLeitura.readLine();
			while(linha != null){
				System.out.println(linha);
				linha = bufferLeitura.readLine();
	
			}
			
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(), "ERRO",
					JOptionPane.ERROR_MESSAGE);
		}
		}
	
		public void mataProcesso(String processo){
			String cmdPid = "taskkill /PID";
			String cmdNome = "taskkill /IM";
			StringBuffer sb = new StringBuffer();
			try{
			@SuppressWarnings("unused")
			int pid = Integer.parseInt(processo);
			sb.append(cmdPid);
			sb.append(" ");
			sb.append(processo);
			}catch(Exception e){
				sb.append(cmdNome);
				sb.append(" ");
				sb.append(processo);
			}
			try {
				Runtime.getRuntime().exec(sb.toString());
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage(), "ERRO",
						JOptionPane.ERROR_MESSAGE);
			}
			
			
		}
	}
	
	
	
	
	
	
	

